package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.dto.OrderSubmitDto;
import com.itheima.common.entity.Result;
import com.itheima.common.vo.OrderSuccessVo;
import com.itheima.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @className OrderController
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 提交预约
     * @param orderSubmitDto 预约信息
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitDto orderSubmitDto) {
        Integer result = orderService.submitOrder(orderSubmitDto);

        if (result > 0) {
            return new Result(true, MessageConstant.ORDER_SUCCESS, result);
        }

        String message;
        switch (result) {
            case -1:
                //会员不存在
                message = MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL;
                break;
            case -2:
                //预约人满
                message = MessageConstant.ORDERSETTING_FAIL;
                break;
            case -3:
                //验证码错误
                message = MessageConstant.VALIDATECODE_ERROR;
                break;
            default:
                throw new IllegalStateException("未知错误: " + result);
        }
        return new Result(false, message);
    }

    /**
     * 预约成功后，根据id查询会员信息
     * @param orderId 预约id
     * @return
     */
    @PostMapping("/findById")
    public Result getMemberInfoById(@RequestParam("id") Integer orderId) {
        return Optional.ofNullable(orderService.getMemberInfoById(orderId))
                .map(order ->  new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order))
                .orElseGet(() -> new Result(false, MessageConstant.QUERY_ORDER_FAIL));
    }
}
